package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.view.Gravity;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.core.content.res.ResourcesCompat;

import com.xh3140.xwidget.dialog.params.LoadingParams;

@SuppressWarnings("unused")
public class LoadingView extends LinearLayoutCompat {
  private final AppCompatTextView mSubtitleView;
  private ProgressBar mProgressBar;

  public LoadingView(@NonNull Context context) {
    super(context);
    setOrientation(VERTICAL);
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
    mSubtitleView = new AppCompatTextView(context);
  }

  /**
   * 获取进度条视图
   */
  public ProgressBar getProgressBar() {
    return mProgressBar;
  }

  /**
   * 获取文本框视图
   */
  public AppCompatTextView getSubtitleView() {
    return mSubtitleView;
  }

  /**
   * 配置视图
   *
   * @param params 加载主体视图配置属性
   */
  public LoadingView configView(@NonNull LoadingParams params) {
    if (getChildCount() > 0) {
      removeAllViews();
    }
    configProgressBar(params);
    configTextView(params);
    return this;
  }

  /**
   * 配置进度条视图
   *
   * @param params 加载主体视图配置属性
   */
  private void configProgressBar(@NonNull LoadingParams params) {
    mProgressBar = new ProgressBar(getContext(), null,
            params.spinner ? android.R.attr.progressBarStyle : android.R.attr.progressBarStyleHorizontal);
    if (params.drawable == 0) {
      if (params.tintColor != 0) {
        mProgressBar.setIndeterminateTintList(ColorStateList.valueOf(params.tintColor));
      }
    } else {
      mProgressBar.setProgressDrawableTiled(ResourcesCompat.getDrawable(getResources(), params.drawable, null));
    }
    final LayoutParams layoutParams = new LayoutParams(params.spinner ? LayoutParams.WRAP_CONTENT : LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    if (params.margins != null) {
      layoutParams.setMarginStart(params.margins[0]);
      layoutParams.topMargin = params.margins[1];
      layoutParams.setMarginEnd(params.margins[2]);
      layoutParams.bottomMargin = params.margins[3];
    }
    mProgressBar.setLayoutParams(layoutParams);
    mProgressBar.setIndeterminate(params.infinite);
    addView(mProgressBar);
  }

  /**
   * 配置文本框视图
   *
   * @param params 加载主体视图配置属性
   */
  private void configTextView(@NonNull LoadingParams params) {
    mSubtitleView.setText(params.text);
    mSubtitleView.setTextSize(params.textSize);
    mSubtitleView.setTextColor(params.textColor);
    mSubtitleView.setTypeface(mSubtitleView.getTypeface(), params.typeface);
    mSubtitleView.setGravity(params.gravity);
    mSubtitleView.setBackgroundColor(params.backgroundColor);
    if (params.padding != null) {
      // TODO
      final int[] padding = params.padding;
      if (params.isTopSubtitle) {
        setPaddingRelative(padding[0], padding[3], padding[2], padding[1]);
      } else {
        setPaddingRelative(padding[0], padding[1], padding[2], padding[3]);
      }
    }
    final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    mSubtitleView.setLayoutParams(layoutParams);
    mSubtitleView.setVisibility(params.text == null ? GONE : VISIBLE);
    addView(mSubtitleView, params.isTopSubtitle ? 0 : -1);
  }
}
